'use strict'

define(['jquery', 'bootstrap', 'select2', 'validate', 'validate.en', 'koala-search', 
        'modalSearch', 'datetimepicker', 'datetimepickerCN', 'shiftform'], function ($, validate) {
  return {

    init: function () {
      
      this.existCode = true;
      this.existCitizen = true;
      this.existMobile = true;
      
      this.initPage();
      this.initCreate();
      this.initVehicle();
      this.exist();
      
    },

    initPage: function () {
      $(document).find('form').bind('reset', function(e){
        $(this).find(".select2").select2("val", "");
      })
    },

    initCreate: function () {
      
      var that = this;

      var $form = $(document).find('form');
      $form.bt_validate();
      
      $('select[name="organization"]').select2({
        theme: "classic",
        placeholder: '请选择',
        minimumResultsForSearch: -1,
        allowClear: true,
      });

      $('select[name="gender"]').select2({
        placeholder: '请选择',
        minimumResultsForSearch: -1,
        allowClear: true,
      });

      $('select[name="educationLevel"]').select2({
        placeholder: '请选择',
        minimumResultsForSearch: -1,
        allowClear: true,
      });

      $('select[name="dutyId"]').select2({
        placeholder: '请选择',
        minimumResultsForSearch: -1,
        allowClear: true,
      });

      $('select[name="clanId"]').select2({
        placeholder: '请选择',
        minimumResultsForSearch: -1,
        allowClear: true,
      });

      $('select[name="degreeId"]').select2({
        placeholder: '请选择',
        minimumResultsForSearch: -1,
        allowClear: true
      });

      $('select[name="drivingType"]').select2({
        placeholder: '请选择',
        minimumResultsForSearch: -1,
        allowClear: true
      });
      
      $('select[name="contractParty"]').select2({
        placeholder: '请选择',
        minimumResultsForSearch: -1,
        allowClear: true
      });
      
      $('.date').datetimepicker({
        format: 'yyyy-mm-dd',
        autoclose: true,
        language: 'zh-CN',
        todayBtn: 'linked',
        startDate: '${today}',
        todayHighlight: true,
        pickerPosition: 'bottom-right',
        minView: 'month'
      });
      
      $form.shiftform({
        beforeSubmit: function (el) {
          
          $form.trigger("validate.ajax.submit");
          
          if (that.existCode || that.existCitizen || that.existMobile) {
            return false;
          }
          return $.bt_validate.result[$.bt_validate.form_id];
          
        }
      });
      
    },
    
    initVehicle: function() {
      
      $('[data-target="#modal-search-vehicle"]').modalsearch({
        url: function(){
          var url = window.__CTX__.root + '/vehicles/query';
          return url + '?companyIds=' + $('input[name="companyId"]').val();
        },
        tableColumns: ['驳运车代码', '驳运车名称'],
        tableValues: ['code', 'name'],
        displayValues: ['code', 'name'],
        values: ['id']
      });
      
      var options = {
        time: 1000,
        url: window.__CTX__.root + "/vehicles/query",
        queryName: "code",
        value: ["id"],
        display: ["code", "name"]
      }
      $('.vehicle-search').koalaSearch(options);

    },
    
    exist: function() {
      
      var that = this;
      
      // check code whether exist
      $(document).on('blur', 'input[name="code"]', function(e) {
        
        var $btnSubmit = $('button.btn-submit');
        
        var code = $('input[name="code"]').val().trim();
        if (!code) return;
          
        $.ajax({
          'url': window.__CTX__.root + "/drivers/exist-code",
          'data': {
            'code': code
          }
        }).done(function(res){
          
          res = typeof res === 'String' ? JSON.parse(res) : res;
          if (res.data.existed) {
            $("input[name='code']").show_err_tooltip(res.data.message);
            $btnSubmit.prop('disabled', true);
            that.existCode = true;
            return false;
          }
          
          that.existCode = false;
          $btnSubmit.prop('disabled', false);
          
        }).fail(function(){
          $("input[name='code']").show_err_tooltip('校验工号唯一性失败！');
          $btnSubmit.prop('disabled', true);
        });

      });
      
      // check citizenCode whether exist
      $(document).on('blur', 'input[name="citizenCode"]', function(e) {
        
        var $btnSubmit = $('button.btn-submit');
        
        var citizenCode = $('input[name="citizenCode"]').val().trim();
        if (!citizenCode) return;
          
        $.ajax({
          'url': window.__CTX__.root + "/drivers/exist-citizen",
          'data': {
            'citizenCode': citizenCode
          }
        }).done(function(res){
          
          res = typeof res === 'String' ? JSON.parse(res) : res;
          if (res.data.existed) {
            $("input[name='citizenCode']").show_err_tooltip(res.data.message);
            $btnSubmit.prop('disabled', true);
            that.existCitizen = true;
            return false;
          }
          
          that.existCitizen = false;
          $btnSubmit.prop('disabled', false);
          
        }).fail(function(){
          $("input[name='citizenCode']").show_err_tooltip('校验身份证号唯一性失败！');
          $btnSubmit.prop('disabled', true);
        });
        
      });  
        
      // check mobile whether exist
      $(document).on('blur', 'input[name="mp"]', function(e) {
        
        var $btnSubmit = $('button.btn-submit');
        
        var mobile = $('input[name="mp"]').val().trim();
        if (!mobile) return;
          
        $.ajax({
          'url': window.__CTX__.root + "/drivers/exist-mobile",
          'data': {
            'mobile': mobile
          }
        }).done(function(res){
          
          res = typeof res === 'String' ? JSON.parse(res) : res;
          if (res.data.existed) {
            $("input[name='mp']").show_err_tooltip(res.data.message);
            $btnSubmit.prop('disabled', true);
            that.existMobile = true;
            return false;
          }
          
          that.existMobile = false;
          $btnSubmit.prop('disabled', false);
          
        }).fail(function(){
          $("input[name='mp']").show_err_tooltip('校验手机号唯一性失败！');
          $btnSubmit.prop('disabled', true);
        });

      });
      
    }

  }
})